from datetime import datetime
from database import db
from .base import BaseModel

class ProductionBatch(BaseModel):
    """Model for production batches."""
    __tablename__ = 'production_batches'

    id = db.Column(db.Integer, primary_key=True)
    batch_number = db.Column(db.String(50), unique=True, nullable=False)
    product_type = db.Column(db.String(100), nullable=False)
    start_time = db.Column(db.DateTime, default=datetime.utcnow)
    end_time = db.Column(db.DateTime)
    status = db.Column(db.String(20), default='in_progress')  # in_progress, completed, aborted
    target_length = db.Column(db.Float)  # Target length in meters
    actual_length = db.Column(db.Float)  # Actual produced length in meters

    def __repr__(self):
        return f'<ProductionBatch {self.batch_number}>'

    def to_dict(self):
        return {
            'id': self.id,
            'batch_number': self.batch_number,
            'product_type': self.product_type,
            'start_time': self.start_time.isoformat() if self.start_time else None,
            'end_time': self.end_time.isoformat() if self.end_time else None,
            'status': self.status,
            'target_length': self.target_length,
            'actual_length': self.actual_length
        }